package com.xingxuecn.demos.mybatis.plugin;

import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;

import java.sql.Statement;
import java.util.Properties;

/**
 * @author Kris at 2018/9/20 15:28
 */
@Intercepts({@Signature(
        type = ResultSetHandler.class,
        method = "handleResultSets",
        args = {Statement.class}
        )})
public class DemoPlugin implements Interceptor {
    /**
     *
     * @param invocation
     * @return
     * @throws Throwable
     */
    public Object intercept(Invocation invocation) throws Throwable {
        //System.out.println(invocation.getArgs());
        return invocation.proceed();
    }

    /**
     * 构造代理对象
     * @param target
     * @return
     */
    public Object plugin(Object target) {
        //System.out.println("plugin");
        return Plugin.wrap(target, this);
    }

    /**
     * 放置xml中配置的plugin标签中的property进来
     * @param properties xml中配置的property
     */
    public void setProperties(Properties properties) {
        System.out.println("setProperties");
    }
}
